/*
 * Created on 4. sep.. 2006
 * Created by Tomas
 */
package people;

import java.util.List;

import junit.framework.TestCase;

import org.junit.Before;
import org.junit.Test;


public class GroupTest extends TestCase {
	private Person adam, eve, cain, abel;
	private Group parents, sons;
	
	@Before
	public void setUp() throws Exception {
		adam 	= new Person(1, "Adam", null);
		eve 	= new Person(2, "Eve", null);
		cain 	= new Person(3, "Cain", null);
		abel 	= new Person(4, "Abel", null);
		parents = new Group("Parents");
		sons 	= new Group("Sons");
	}

	@Test
	public final void testScoring() {
		assertEquals(0, parents.getScore());
		assertEquals(0, sons.getScore());
		
		parents.setScore(200);
		sons.setScore(100);
		assertEquals(200, parents.getScore());
		assertEquals(100, sons.getScore());
		
	}

	@Test
	public final void testPersonScoreKeeping() {
		testScoring();
		
		assertEquals(0, adam.getScore());
		parents.addPerson(adam);
		assertEquals(parents.getScore(), adam.getScore());
		sons.addPerson(adam);
		assertEquals(parents.getScore() + sons.getScore(), adam.getScore());
		
		try {
			parents.removePerson(adam);
		} catch (PersonNotFoundException e) {
			// This should not happen
			fail();
		}
		assertEquals(sons.getScore(), adam.getScore());
		
		//re-adding to the same group shouldn't work
		sons.addPerson(adam);
		assertEquals(sons.getScore(), adam.getScore());
		
		//removing from a group not affiliated with shouldn't work
		try {
			parents.removePerson(adam);
			//should throw exception
			fail();
		} catch (PersonNotFoundException e) {
			//This should happen
		}
		assertEquals(sons.getScore(), adam.getScore());
		
		
	}


	@Test
	public final void testGetPersons() {
		parents.addPerson(adam);
		parents.addPerson(eve);
		sons.addPerson(cain);
		sons.addPerson(abel);
		
		List<Person> liParents = parents.getPersons();
		List<Person> liSons = sons.getPersons();
		
		assertTrue(liParents.contains(adam));
		assertTrue(liParents.contains(eve));
		assertEquals(false, liParents.contains(cain));
		assertTrue(liSons.contains(abel));
		assertTrue(liSons.contains(new Person(3, "Cain", null)));
	}

	@Test
	public final void testInGroup() {
		parents.addPerson(adam);
		parents.addPerson(eve);
		sons.addPerson(cain);
		sons.addPerson(abel);
		
		assertEquals(true, parents.inGroup(adam));
		assertEquals(true, parents.inGroup(eve));
		assertEquals(true, sons.inGroup(cain));
		assertEquals(true, sons.inGroup(abel));
		
		assertEquals(false, parents.inGroup(cain));
		assertEquals(false, parents.inGroup(abel));
		assertEquals(false, sons.inGroup(adam));
		assertEquals(false, sons.inGroup(eve));
		assertEquals(false, sons.inGroup(new Person(8, "Bob", null)));
		
		
	}

}
